
package gameoflife;

/**
 * Welcome to the Generation class.
 *
 * A generation object holds the state of one generation in the game of life
 * simulation
 * @author CJ
 */
public class Generation {

    //Instance Variables
    private int[] generation;


    //Constructors
    /**
     * Basic constructor. Constructs a generation of the desired size.
     * Note: Generation at init is all zero.
     * Assumes that generationSize if valid, > 0
     * 
     * @param generationSize, size of the generation to be init
     */
    public Generation(int generationSize){
       this.generation = new int[generationSize];
    }


    //Methods
    /**
     * Getter method of a single individual within the generation.
     *
     * Assumes that index is valid for the generation;
     *
     * @param index of desired individual.
     * @return individual's value, 0 or 1
     */
    public int getIndividual(int index){
        return this.generation[index];
    }

    /**
     * Setter method of a single individual within the generation.
     *
     * Assumes that index is valid for the generation.
     *
     * @param index of desired individual
     * @param value to be assigned.
     */
    public void setIndividual(int index, int value){

        //Sanity Check
        if(value > 1 || value < 0){
            throw new IllegalArgumentException("In setIndividual:Generation," +
                    "passed in value is non-valid.");
        }

        this.generation[index] = value;
    }

    /**
     * Basic getter method that returns the generation size.
     * 
     * @return size of generation
     */
    public int getGenerationSize(){
        return this.generation.length;
    }

    /**
     * Sets this generation to the passed in string.  Mainly for debugging
     * purposes.
     * @param inputString that this generation is to be set to
     */
    public void setToString(String inputString){
        this.generation = new int[inputString.length()];
        for(int i = 0; i < this.generation.length; ++i){
            this.generation[i] = Integer.parseInt("" + inputString.charAt(i));
        }
    }

    /**
     * Overrides the toString() method in order to easily facilitate ease of
     * outputing to the terminal
     *
     * @return, The String represented by this generation, not ended by a newline
     */
    public String toString(){
        String result = "";

        //Transverse the generation adding each member to the result string
        for(int i = 0; i < this.generation.length; ++i){
            result += this.generation[i];
        }

        return result;
    }

}
